// MessageKernel.cpp : 定义 DLL 应用程序的导出函数。
//
#include "source/bond/message_bond_impl.h"
#include "source/message_kernel_impl.h"
#include "source/process/message_process_impl.h"
#include "source/sync/message_sync_impl.h"

using namespace qb;

#ifdef _MSC_VER

struct CClassFactory : public IClassFactory {
  virtual HRESULT STDMETHODCALLTYPE CreateInstance(IUnknown* pUnkOuter,
                                                   REFIID riid,
                                                   void** ppvObject) {
    if (riid == IID_IMessageKernel) {
      CMessageKernel* p = new CUnknownObject<CMessageKernel>;
      (static_cast<IMessageKernel*>(p))->AddRef();

      *ppvObject = p;

      return S_OK;
    } else if (riid == IID_IMessageBond) {
      CMessageBondImpl* p = new CUnknownObject<CMessageBondImpl>;
      (static_cast<IMessageBond*>(p))->AddRef();

      *ppvObject = p;

      return S_OK;
    } else if (riid == IID_IMessageProcess) {
      CMessageProcessImpl* p = new CUnknownObject<CMessageProcessImpl>;
      (static_cast<IMessageProcess*>(p))->AddRef();

      *ppvObject = p;

      return 0;
    } else if (riid == IID_IMessageSync) {
      CMessageSyncImpl* p = new CUnknownObject<CMessageSyncImpl>;
      (static_cast<IMessageSync*>(p))->AddRef();

      *ppvObject = p;

      return 0;
    }

    return E_NOTIMPL;
  }

  virtual HRESULT STDMETHODCALLTYPE LockServer(BOOL fLock) { return E_NOTIMPL; }
};

extern "C" S_QBMESSGAE_EXPORT HRESULT __stdcall QtDllGetClassObject(
    REFCLSID rclsid, REFIID riid, LPVOID* ppv) {
  if (CLSID_MessageKernel == rclsid) {
    if (IID_IClassFactory == riid) {
      CClassFactory* pf = new CUnknownObject<CClassFactory>;
      pf->AddRef();
      *ppv = pf;
      return S_OK;
    }
  }
  return E_NOTIMPL;
}

#else

STDAPI MessageKernel_CreateObject(REFCLSID rclsid, REFIID riid,
                                  LPVOID* ppvObject) {
  if (CLSID_MessageKernel == rclsid) {
    if (riid == IID_IMessageKernel) {
      CMessageKernel* p = new CUnknownObject<CMessageKernel>;
      (static_cast<IMessageKernel*>(p))->AddRef();

      *ppvObject = p;

      return 0;
    } else if (riid == IID_IMessageBond) {
      CMessageBondImpl* p = new CUnknownObject<CMessageBondImpl>;
      (static_cast<IMessageBond*>(p))->AddRef();

      *ppvObject = p;

      return 0;
    } else if (riid == IID_IMessageProcess) {
      CMessageProcessImpl* p = new CUnknownObject<CMessageProcessImpl>;
      (static_cast<IMessageProcess*>(p))->AddRef();

      *ppvObject = p;

      return 0;
    } else if (IID_IMessageSync == riid) {
      CMessageSyncImpl* p = new CUnknownObject<CMessageSyncImpl>;
      (static_cast<IMessageSync*>(p))->AddRef();

      *ppvObject = p;

      return 0;
    }
  }
  return -1;
}

#endif
